package com.hhsjtest.segiui

import android.content.res.AssetManager
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.hhsjtest.bus_enter.businessdata.Enter
import com.hhsjdesign.dynamicui.utils.WidgetDataParser
import com.hhsjdesign.dynamicui.utils.WidgetManager
import com.hhsjdesign.dynamicui.businessdata.BusinessDataParser
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.DynamicUI
import com.hhsjdesign.dynamicui.core.property.ViewBaseProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst
import com.hhsjdesign.dynamicui.utils.BusinessViewCreatorFactory
import com.hhsjdesign.dynamicui.utils.DataCallerFactory
import com.hhsjdesign.segiui.R
import org.json.JSONArray
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder

class MenuActivity : AppCompatActivity() {
    lateinit var widgetManager: WidgetManager
    lateinit var topbarWidgetManager: WidgetManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_menu)
        topbarWidgetManager = WidgetManager.createWidgetManager(this)
        topbarWidgetManager.makeWidget(
            ArrayList<WidgetData>()
                .apply {
                    add(
                        WidgetData(JSONObject().apply {
                            put("contentType", 1)//标题
                            put("contentAlign", 0)//居中
                            put("isVariableArea", 1)//事件
                            put("foldedMenus", JSONArray().apply {
                                //点击事件
                            })
                        })
                            .apply {
                                this.widgetId = "1111"
                                this.type = WidgetTypeConst.GRID_HORIZONTAL
                                //标题，事件
                                this.type = WidgetTypeConst.SPEC_BUSINESS_TOP_NAVIGATION_BAR_V1
                                this.category = WidgetData.WIDGET_CATEGORY_COMMON_BUSINESS
                                this.business = "enter"
                                //右侧时间区域
                                this.businessData = ArrayList<IViewable<*>>().apply {
                                    add(IViewable<Enter>().apply {
                                        this.businessData = Enter().apply {
                                            url = ""
                                            msgCode = "RES_MESSAGE"
                                            icon = ""
                                        }
                                    })
                                }

                                this.viewProperty = ViewBaseProperty().apply {

                                }

                            })
                }, "main"
        )
        DynamicUI.inject(DynamicUIRegister())

        Log.e("HHSJH", "BusinessDataParser.mapSize = ${BusinessDataParser.mapSize()}")
        Log.e(
            "HHSJH",
            "BusinessViewCreatorFactory.mapSize = ${BusinessViewCreatorFactory.mapSize()}"
        )
        Log.e("HHSJH", "DataCallerFactory.mapSize = ${DataCallerFactory.mapSize()}")

        val assetManager: AssetManager = assets

        val inputStream = assetManager.open("test1.json")

        val stringBuilder = StringBuilder()

        val reader = BufferedReader(InputStreamReader(inputStream))

        var line: String?
        while ((reader.readLine().also { line = it }) != null) {
            stringBuilder.append(line)
        }
        reader.close()
        inputStream.close()

        val jo = JSONArray(stringBuilder.toString())
        val widgetList: MutableList<WidgetData> = ArrayList()

        for (i in 0 until jo.length()) {
            val widget: WidgetData? =
                WidgetDataParser.parseWidget(jo.optJSONObject(i), WidgetTypeConst.LAYOUT_LINEAR_VERTICAL)
            widget?.let {
                widgetList.add(it)
            }
        }
        widgetManager = WidgetManager.createWidgetManager(this)
        widgetManager.makeWidget(
            widgetList, "main"
        )
        widgetManager.attachToParent(findViewById<LinearLayout>(R.id.content_layout))

        //刷新红点
        Handler(Looper.getMainLooper()).postDelayed({
        }, 2000)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            widgetManager.loadData()
        }
    }
}